﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_NewsDAL : IDcms_NewsDAL
    {
        public Dcms_NewsModel AddModel(Dcms_NewsModel model)
        {
            string sQLString = "insert into Dcms_News(News_CateId,News_Title,News_Image,News_Video,News_Count,News_Content,News_State,News_Author,News_Source,News_IsIndex,News_IsImage,News_IsUrl,News_Url,News_Target,News_Tags,News_AddTime,News_ExFlag1,News_ExFlag2,News_ExFlag3,News_ExFlag4,News_ExFlag5,News_ExFlag6,News_ExFlag7,News_ExFlag8,News_ExFlag9,News_ExFlag10) values(@News_CateId,@News_Title,@News_Image,@News_Video,@News_Count,@News_Content,@News_State,@News_Author,@News_Source,@News_IsIndex,@News_IsImage,@News_IsUrl,@News_Url,@News_Target,@News_Tags,@News_AddTime,@News_ExFlag1,@News_ExFlag2,@News_ExFlag3,@News_ExFlag4,@News_ExFlag5,@News_ExFlag6,@News_ExFlag7,@News_ExFlag8,@News_ExFlag9,@News_ExFlag10) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@NewId", SqlDbType.Int), new SqlParameter("@News_CateId", SqlDbType.Int), new SqlParameter("@News_Title", SqlDbType.VarChar), new SqlParameter("@News_Image", SqlDbType.VarChar), new SqlParameter("@News_Video", SqlDbType.VarChar), new SqlParameter("@News_Count", SqlDbType.Int), new SqlParameter("@News_Content", SqlDbType.Text), new SqlParameter("@News_State", SqlDbType.VarChar), new SqlParameter("@News_Author", SqlDbType.VarChar), new SqlParameter("@News_Source", SqlDbType.VarChar), new SqlParameter("@News_IsIndex", SqlDbType.TinyInt), new SqlParameter("@News_IsImage", SqlDbType.TinyInt), new SqlParameter("@News_IsUrl", SqlDbType.TinyInt), new SqlParameter("@News_Url", SqlDbType.VarChar), new SqlParameter("@News_Target", SqlDbType.VarChar), new SqlParameter("@News_Tags", SqlDbType.VarChar), 
                new SqlParameter("@News_AddTime", SqlDbType.DateTime), new SqlParameter("@News_ExFlag1", SqlDbType.VarChar), new SqlParameter("@News_ExFlag2", SqlDbType.VarChar), new SqlParameter("@News_ExFlag3", SqlDbType.VarChar), new SqlParameter("@News_ExFlag4", SqlDbType.VarChar), new SqlParameter("@News_ExFlag5", SqlDbType.VarChar), new SqlParameter("@News_ExFlag6", SqlDbType.VarChar), new SqlParameter("@News_ExFlag7", SqlDbType.VarChar), new SqlParameter("@News_ExFlag8", SqlDbType.VarChar), new SqlParameter("@News_ExFlag9", SqlDbType.VarChar), new SqlParameter("@News_ExFlag10", SqlDbType.VarChar)
             };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.News_CateId;
            cmdParms[2].Value = model.News_Title;
            cmdParms[3].Value = model.News_Image;
            cmdParms[4].Value = model.News_Video;
            cmdParms[5].Value = model.News_Count;
            cmdParms[6].Value = model.News_Content;
            cmdParms[7].Value = model.News_State;
            cmdParms[8].Value = model.News_Author;
            cmdParms[9].Value = model.News_Source;
            cmdParms[10].Value = model.News_IsIndex;
            cmdParms[11].Value = model.News_IsImage;
            cmdParms[12].Value = model.News_IsUrl;
            cmdParms[13].Value = model.News_Url;
            cmdParms[14].Value = model.News_Target;
            cmdParms[15].Value = model.News_Tags;
            cmdParms[0x10].Value = model.News_AddTime;
            cmdParms[0x11].Value = model.News_ExFlag1;
            cmdParms[0x12].Value = model.News_ExFlag2;
            cmdParms[0x13].Value = model.News_ExFlag3;
            cmdParms[20].Value = model.News_ExFlag4;
            cmdParms[0x15].Value = model.News_ExFlag5;
            cmdParms[0x16].Value = model.News_ExFlag6;
            cmdParms[0x17].Value = model.News_ExFlag7;
            cmdParms[0x18].Value = model.News_ExFlag8;
            cmdParms[0x19].Value = model.News_ExFlag9;
            cmdParms[0x1a].Value = model.News_ExFlag10;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.News_Id = (int) cmdParms[0].Value;
            return model;
        }

        public bool CopyNews(int SourceCateID, int DestCateID)
        {
            string sQLString = "insert into Dcms_News (News_CateId, News_Title, News_Image, News_Video, News_Count, News_Content, News_State, News_Author, News_Source, News_IsIndex, News_IsImage, News_IsUrl, News_Url, News_Target, News_Tags, News_AddTime, News_ExFlag1, News_ExFlag2, News_ExFlag3, News_ExFlag4, News_ExFlag5, News_ExFlag6, News_ExFlag7, News_ExFlag8, News_ExFlag9, News_ExFlag10)";
            sQLString = sQLString + "select @DestCateID, News_Title, News_Image, News_Video, News_Count, News_Content, News_State, News_Author, News_Source, News_IsIndex, News_IsImage, News_IsUrl, News_Url, News_Target, News_Tags, News_AddTime, News_ExFlag1, News_ExFlag2, News_ExFlag3, News_ExFlag4, News_ExFlag5, News_ExFlag6, News_ExFlag7, News_ExFlag8, News_ExFlag9, News_ExFlag10 from Dcms_News where News_CateId=@SourceCateID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SourceCateID", SqlDbType.Int), new SqlParameter("@DestCateID", SqlDbType.Int) };
            cmdParms[0].Value = SourceCateID;
            cmdParms[1].Value = DestCateID;
            return SqlHelper.ExecuteSql(sQLString, cmdParms) > 0;
        }

        public IList<Dcms_NewsModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_NewsModel> list = new List<Dcms_NewsModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_NewsModel item = new Dcms_NewsModel();
                if (ds.Tables[0].Rows[i]["News_Id"].ToString() != "") item.News_Id = int.Parse(ds.Tables[0].Rows[i]["News_Id"].ToString());
                if (ds.Tables[0].Rows[i]["News_CateId"].ToString() != "") item.News_CateId = int.Parse(ds.Tables[0].Rows[i]["News_CateId"].ToString());
                item.News_Title = ds.Tables[0].Rows[i]["News_Title"].ToString();
                item.News_Image = ds.Tables[0].Rows[i]["News_Image"].ToString();
                item.News_Video = ds.Tables[0].Rows[i]["News_Video"].ToString();
                if (ds.Tables[0].Rows[i]["News_Count"].ToString() != "") item.News_Count = int.Parse(ds.Tables[0].Rows[i]["News_Count"].ToString());
                item.News_Content = ds.Tables[0].Rows[i]["News_Content"].ToString();
                item.News_State = ds.Tables[0].Rows[i]["News_State"].ToString();
                item.News_Author = ds.Tables[0].Rows[i]["News_Author"].ToString();
                item.News_Source = ds.Tables[0].Rows[i]["News_Source"].ToString();
                if (ds.Tables[0].Rows[i]["News_IsIndex"].ToString() != "") item.News_IsIndex = int.Parse(ds.Tables[0].Rows[i]["News_IsIndex"].ToString());
                if (ds.Tables[0].Rows[i]["News_IsImage"].ToString() != "") item.News_IsImage = int.Parse(ds.Tables[0].Rows[i]["News_IsImage"].ToString());
                if (ds.Tables[0].Rows[i]["News_IsUrl"].ToString() != "") item.News_IsUrl = int.Parse(ds.Tables[0].Rows[i]["News_IsUrl"].ToString());
                item.News_Url = ds.Tables[0].Rows[i]["News_Url"].ToString();
                item.News_Target = ds.Tables[0].Rows[i]["News_Target"].ToString();
                item.News_Tags = ds.Tables[0].Rows[i]["News_Tags"].ToString();
                if (ds.Tables[0].Rows[i]["News_AddTime"].ToString() != "") item.News_AddTime = DateTime.Parse(ds.Tables[0].Rows[i]["News_AddTime"].ToString());
                item.News_ExFlag1 = ds.Tables[0].Rows[i]["News_ExFlag1"].ToString();
                item.News_ExFlag2 = ds.Tables[0].Rows[i]["News_ExFlag2"].ToString();
                item.News_ExFlag3 = ds.Tables[0].Rows[i]["News_ExFlag3"].ToString();
                item.News_ExFlag4 = ds.Tables[0].Rows[i]["News_ExFlag4"].ToString();
                item.News_ExFlag5 = ds.Tables[0].Rows[i]["News_ExFlag5"].ToString();
                item.News_ExFlag6 = ds.Tables[0].Rows[i]["News_ExFlag6"].ToString();
                item.News_ExFlag7 = ds.Tables[0].Rows[i]["News_ExFlag7"].ToString();
                item.News_ExFlag8 = ds.Tables[0].Rows[i]["News_ExFlag8"].ToString();
                item.News_ExFlag9 = ds.Tables[0].Rows[i]["News_ExFlag9"].ToString();
                item.News_ExFlag10 = ds.Tables[0].Rows[i]["News_ExFlag10"].ToString();
                list.Add(item);
            }
            return list;
        }

        public bool DeleteAllCheck(string AllID)
        {
            return SqlHelper.ExecuteSql("delete from Dcms_News where News_Id In (" + AllID + ")", null) > 0;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_News where News_Id=@News_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@News_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public string GetDataCount(string SqlStr)
        {
            return SqlHelper.GetNoteCount(SqlStr).ToString();
        }

        public IList<Dcms_NewsModel> GetDataListByPage(string SqlStr, int CurPage, int PageSize)
        {
            return this.DataSetToList(SqlHelper.CreateDataSet(SqlStr, (CurPage - 1) * PageSize, PageSize));
        }

        public Dcms_NewsModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_News where News_Id=@News_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@News_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            Dcms_NewsModel model = new Dcms_NewsModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_NewsModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelList()
        {
            string sql = "select * from Dcms_News";
            return SqlHelper.CreateDataSet(sql);
        }

        public Dcms_NewsModel GetNewModelByCatID(int CatID)
        {
            return null;
        }

        public bool MoveNews(int SourceCateID, int DestCateID)
        {
            string sQLString = "update Dcms_News set News_CateId=@DestCateID where News_CateId=@SourceCateID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SourceCateID", SqlDbType.Int), new SqlParameter("@DestCateID", SqlDbType.Int) };
            cmdParms[0].Value = SourceCateID;
            cmdParms[1].Value = DestCateID;
            return SqlHelper.ExecuteSql(sQLString, cmdParms) > 0;
        }

        public Dcms_NewsModel Update(Dcms_NewsModel model)
        {
            string sQLString = "update Dcms_News set News_CateId=@News_CateId,News_Title=@News_Title,News_Image=@News_Image,News_Video=@News_Video,News_Count=@News_Count,News_Content=@News_Content,News_State=@News_State,News_Author=@News_Author,News_Source=@News_Source,News_IsIndex=@News_IsIndex,News_IsImage=@News_IsImage,News_IsUrl=@News_IsUrl,News_Url=@News_Url,News_Target=@News_Target,News_Tags=@News_Tags,News_AddTime=@News_AddTime,News_ExFlag1=@News_ExFlag1,News_ExFlag2=@News_ExFlag2,News_ExFlag3=@News_ExFlag3,News_ExFlag4=@News_ExFlag4,News_ExFlag5=@News_ExFlag5,News_ExFlag6=@News_ExFlag6,News_ExFlag7=@News_ExFlag7,News_ExFlag8=@News_ExFlag8,News_ExFlag9=@News_ExFlag9,News_ExFlag10=@News_ExFlag10 where News_Id=@News_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@News_CateId", SqlDbType.Int), new SqlParameter("@News_Title", SqlDbType.VarChar), new SqlParameter("@News_Image", SqlDbType.VarChar), new SqlParameter("@News_Video", SqlDbType.VarChar), new SqlParameter("@News_Count", SqlDbType.Int), new SqlParameter("@News_Content", SqlDbType.Text), new SqlParameter("@News_State", SqlDbType.VarChar), new SqlParameter("@News_Author", SqlDbType.VarChar), new SqlParameter("@News_Source", SqlDbType.VarChar), new SqlParameter("@News_IsIndex", SqlDbType.TinyInt), new SqlParameter("@News_IsImage", SqlDbType.TinyInt), new SqlParameter("@News_IsUrl", SqlDbType.TinyInt), new SqlParameter("@News_Url", SqlDbType.VarChar), new SqlParameter("@News_Target", SqlDbType.VarChar), new SqlParameter("@News_Tags", SqlDbType.VarChar), new SqlParameter("@News_AddTime", SqlDbType.DateTime), 
                new SqlParameter("@News_ExFlag1", SqlDbType.VarChar), new SqlParameter("@News_ExFlag2", SqlDbType.VarChar), new SqlParameter("@News_ExFlag3", SqlDbType.VarChar), new SqlParameter("@News_ExFlag4", SqlDbType.VarChar), new SqlParameter("@News_ExFlag5", SqlDbType.VarChar), new SqlParameter("@News_ExFlag6", SqlDbType.VarChar), new SqlParameter("@News_ExFlag7", SqlDbType.VarChar), new SqlParameter("@News_ExFlag8", SqlDbType.VarChar), new SqlParameter("@News_ExFlag9", SqlDbType.VarChar), new SqlParameter("@News_ExFlag10", SqlDbType.VarChar), new SqlParameter("@News_Id", SqlDbType.Int)
             };
            cmdParms[0].Value = model.News_CateId;
            cmdParms[1].Value = model.News_Title;
            cmdParms[2].Value = model.News_Image;
            cmdParms[3].Value = model.News_Video;
            cmdParms[4].Value = model.News_Count;
            cmdParms[5].Value = model.News_Content;
            cmdParms[6].Value = model.News_State;
            cmdParms[7].Value = model.News_Author;
            cmdParms[8].Value = model.News_Source;
            cmdParms[9].Value = model.News_IsIndex;
            cmdParms[10].Value = model.News_IsImage;
            cmdParms[11].Value = model.News_IsUrl;
            cmdParms[12].Value = model.News_Url;
            cmdParms[13].Value = model.News_Target;
            cmdParms[14].Value = model.News_Tags;
            cmdParms[15].Value = model.News_AddTime;
            cmdParms[0x10].Value = model.News_ExFlag1;
            cmdParms[0x11].Value = model.News_ExFlag2;
            cmdParms[0x12].Value = model.News_ExFlag3;
            cmdParms[0x13].Value = model.News_ExFlag4;
            cmdParms[20].Value = model.News_ExFlag5;
            cmdParms[0x15].Value = model.News_ExFlag6;
            cmdParms[0x16].Value = model.News_ExFlag7;
            cmdParms[0x17].Value = model.News_ExFlag8;
            cmdParms[0x18].Value = model.News_ExFlag9;
            cmdParms[0x19].Value = model.News_ExFlag10;
            cmdParms[0x1a].Value = model.News_Id;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public void UpDateCount(int ID)
        {
            string sQLString = "Update Dcms_News Set News_Count=(News_Count+1) where News_Id=@News_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@News_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public bool UpdateState(string AllID, int StateValue)
        {
            return SqlHelper.ExecuteSql("Update Dcms_News Set News_State='" + StateValue.ToString() + "' where News_Id In (" + AllID + ")", null) > 0;
        }
    }
}

